/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PropertyRental.war.servlets;

import PropertyRental.ejb.beans.AccountFacade;
import PropertyRental.ejb.beans.AgentFacade;
import PropertyRental.ejb.beans.CustomerFacade;
import PropertyRental.ejb.beans.LoginEJB;
import PropertyRental.ejb.beans.OwnerFacade;
import PropertyRental.ejb.beans.PropertyFacade;
import PropertyRental.ejb.beans.PropertyRentFacade;
import PropertyRental.ejb.beans.UsersFacade;
import PropertyRental.ejb.beans.VisitingPropertyFacade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Sébastien
 */
@WebServlet(name="PropertyRentalController", urlPatterns={"*.do"})
public class PropertyRentalController extends HttpServlet {

    @EJB
    private AccountFacade accountEJB;

    @EJB
    private AgentFacade agentEJB;

    @EJB
    private CustomerFacade customerEJB ;

    @EJB
    private LoginEJB loginEJB ;

    @EJB
    private OwnerFacade ownerEJB ;

    @EJB
    private PropertyFacade propertyEJB ;
    

    @EJB
    private PropertyRentFacade propertyrentEJB ;
    
    @EJB
    private UsersFacade userfacadeEJB ;

    @EJB
    private VisitingPropertyFacade visitingpropertyEJB ;

    private HashMap<String,Action> actions;

    @Override
    public void init() throws ServletException {
        actions = new HashMap<String,Action>();

        LoginAction la = new LoginAction("/login.jsp", "/header.jsp");
        la.setLoginEJB(loginEJB);
        actions.put("login", la);

        LogoutAction loa = new LogoutAction("/header.jsp","");
        loa.setLoginEJB(loginEJB);
        actions.put("logout", loa);
        
        ViewAccountAction vaa = new ViewAccountAction("/viewAccount.jsp", "/loginCheckFailed.jsp");
        vaa.setLoginEJB(loginEJB);
        actions.put("viewAccount", vaa);

        AddUserPageAction aupa = new AddUserPageAction("/addUser.jsp", "/loginCheckFailed.jsp");
        aupa.setLoginEJB(loginEJB);
        actions.put("adduserpage", aupa);
        AddUserAction aua = new AddUserAction("/addUser.jsp", "/failedAddUser.jsp");
        aua.setAccountEJB(accountEJB);
        actions.put("adduser", aua);


        DeleteUserAction dua = new DeleteUserAction("/successfulDeleted.jsp", "/failDel.jsp");
        dua.setLoginEJB(loginEJB);
        actions.put("deleteAccount", dua);
        
      //  ProceedTransaction pt = new ProceedTransaction("/ProceedFinancialTransaction.jsp", "");
       // pt.setPropertyEJB(propertyEJB);
        //  actions.put("ProceedFinancialTransaction", pt);

        PropertyAction pra = new PropertyAction("/viewProperty.jsp", "/loginCheckFailed.jsp");
        pra.setPropertyEJB(propertyEJB);
        actions.put("viewProperty", pra);

        OwnerPropertyAction owp = new OwnerPropertyAction("/viewOwnerProperty.jsp", "/loginCheckFailed.jsp");
        owp.setPropertyEJB(propertyEJB);
        actions.put("viewOwnerProperty", owp);

        AddPropertyFromAction apf = new AddPropertyFromAction("/addProperty.jsp", "/loginCheckFailed.jsp");
        actions.put("addPropertyForm", apf);

        AddPropertyAction adp = new AddPropertyAction("/successfulAdd.jsp", "/failedAdd.jsp");
        adp.setPropertyEJB(propertyEJB);
        adp.setOwnerEJB(ownerEJB);
        actions.put("addProperty", adp);

        SearchPropertyFromAction spf = new SearchPropertyFromAction("/searchProperty.jsp", "/loginCheckFailed.jsp");
        actions.put("searchPropertyForm", spf);

        SearchPropertyAction sep = new SearchPropertyAction("/viewProperty.jsp", "/loginCheckFailed.jsp");
        sep.setPropertyEJB(propertyEJB);
        actions.put("searchProperty", sep);

        UpdatePropertyFormAction upf = new UpdatePropertyFormAction("/updateProperty.jsp", "");
        actions.put("updatePropertyForm", upf);

        UpdatePropertyAction upp = new UpdatePropertyAction("/successfulDEl.jsp", "/failedDel.jsp");
        upp.setPropertyEJB(propertyEJB);
        actions.put("updateProperty", upp);

        DeletePropertyAction dep = new DeletePropertyAction("/successfulDel.jsp", "/failedDel.jsp");
        dep.setPropertyEJB(propertyEJB);
        actions.put("deleteProperty", dep);
    }


    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String next = "";
        String op = getOperation(request.getRequestURL());
        Action act = actions.get(op);
        if (act != null) {
            next = act.perform(request);
        }

        // Get the dispatcher
        RequestDispatcher dispatcher =
               getServletContext().getRequestDispatcher(next);


        if (dispatcher != null)
               dispatcher.forward(request, response);
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public String getOperation(StringBuffer requestURL) {
        int lastslash = requestURL.lastIndexOf("/");
        int lastdot = requestURL.lastIndexOf(".");
        String op = requestURL.substring(lastslash+1, lastdot);
        return op;
    }
}
